<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="utf-8" />
    
  <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1" />
  <title>
    ES6.md |  Small Bridge
  </title>
  <meta name="generator" content="hexo-theme-yilia-plus">
  
  <link rel="shortcut icon" href="/favicon.ico" />
  
  
<link rel="stylesheet" href="/css/main.css">

  
  <script src="https://cdn.jsdelivr.net/npm/pace-js@1.0.2/pace.min.js"></script>
  
  

  

<link rel="alternate" href="/atom.xml" title="Small Bridge" type="application/atom+xml">
</head>

</html>

<body>
  <div id="app">
    <main class="content">
      <section class="outer">
  <article id="post-ES6-md" class="article article-type-post" itemscope
  itemprop="blogPost" data-scroll-reveal>

  <div class="article-inner">
    
    <header class="article-header">
       
<h1 class="article-title sea-center" style="border-left:0" itemprop="name">
  ES6.md
</h1>
  

    </header>
    

    
    <div class="article-meta">
      <a href="/2020/04/12/ES6-md/" class="article-date">
  <time datetime="2020-04-12T09:38:20.000Z" itemprop="datePublished">2020-04-12</time>
</a>
      
  <div class="article-category">
    <a class="article-category-link" href="/categories/JavaScript/">JavaScript</a>
  </div>

      
      
<div class="word_count">
    <span class="post-time">
        <span class="post-meta-item-icon">
            <i class="ri-quill-pen-line"></i>
            <span class="post-meta-item-text"> 字数统计:</span>
            <span class="post-count">1.9k字</span>
        </span>
    </span>

    <span class="post-time">
        &nbsp; | &nbsp;
        <span class="post-meta-item-icon">
            <i class="ri-book-open-line"></i>
            <span class="post-meta-item-text"> 阅读时长≈</span>
            <span class="post-count">8分钟</span>
        </span>
    </span>
</div>

      
    </div>
    

    
    
    <div class="tocbot"></div>





    

    
    <div class="article-entry" itemprop="articleBody">
      


      

      
      <p>@<a href="ES6新增语法">TOC</a></p>
<h1 id="什么是ES6"><a href="#什么是ES6" class="headerlink" title="什么是ES6"></a>什么是ES6</h1><a id="more"></a>
<p>全称ECMScript,它是由ECMA国际标准化组织制定的一项脚本语言的标准化规范(范指ES2015-ES2018)</p>
<h1 id="新增关键字let"><a href="#新增关键字let" class="headerlink" title="新增关键字let"></a>新增关键字let</h1><p>新增的用于声明变量的关键字let</p>
<ol>
<li>let声明的变量只在所处于的块级作用域有效(一对大括号中)<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">if</span>(<span class="literal">true</span>)&#123;</span><br><span class="line">      <span class="keyword">let</span>  a = <span class="number">10</span></span><br><span class="line">    &#125;</span><br></pre></td></tr></table></figure></li>
<li>let可以防止循环变量变成全局变量<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">for</span>(<span class="keyword">var</span> i = <span class="number">0</span>; i &lt; <span class="number">10</span>; i++) &#123;</span><br><span class="line">&#125;</span><br><span class="line"><span class="built_in">console</span>.log(i);  <span class="comment">//这里可以访问到i = 10</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">for</span>(<span class="keyword">let</span> j = <span class="number">0</span>; j &lt; <span class="number">10</span>; j++) &#123;</span><br><span class="line">&#125;  <span class="comment">//let不行,undefined</span></span><br></pre></td></tr></table></figure></li>
<li>let声明的变量不存在变量提升<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">console</span>.log(num);</span><br><span class="line"><span class="keyword">let</span> num;    <span class="comment">//会报错</span></span><br></pre></td></tr></table></figure></li>
<li>let声明的变量会形成暂时性死区<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> num = <span class="number">10</span></span><br><span class="line">    <span class="keyword">if</span>(<span class="literal">true</span>)&#123;</span><br><span class="line">        <span class="comment">//num会与let声明的变量进行绑定，从而报错，与外面的var声明的变量num没有关系</span></span><br><span class="line">        <span class="comment">//console.log(num)</span></span><br><span class="line">        <span class="comment">//let num = 10</span></span><br><span class="line">    &#125;</span><br></pre></td></tr></table></figure></li>
<li>看一个经典面试题<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//经典面试题------全局变量i的值被改变，所以两次都打印2</span></span><br><span class="line">    <span class="comment">//若把var改成let，则会打印0，1---&gt;每次循环都会产生一个块级作用域，每个块级作用域中的变量都是不同的，函数执行时输出的是自己上一级(循环产生的块级作用域)作用域下的值</span></span><br><span class="line">    <span class="keyword">var</span> arr = [];</span><br><span class="line">    <span class="keyword">for</span>(<span class="keyword">var</span> i = <span class="number">0</span>; i &lt; <span class="number">2</span>; i++)&#123;</span><br><span class="line">        arr[i] = <span class="function"><span class="keyword">function</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">            <span class="built_in">console</span>.log(i)</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">    arr[<span class="number">0</span>]();</span><br><span class="line">    arr[<span class="number">1</span>]();</span><br></pre></td></tr></table></figure>
<h1 id="新增关键字const"><a href="#新增关键字const" class="headerlink" title="新增关键字const"></a>新增关键字const</h1></li>
<li>块级作用域<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">if</span>(<span class="literal">true</span>)&#123;</span><br><span class="line">      <span class="keyword">const</span> aa = <span class="number">1</span></span><br><span class="line">  &#125;</span><br><span class="line">  <span class="comment">//大括号外面访问不到aa</span></span><br><span class="line">  <span class="comment">//console.log(aa)</span></span><br></pre></td></tr></table></figure></li>
<li>声明常量时必须赋值<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//报错没有赋初始值</span></span><br><span class="line">    <span class="comment">//const PI</span></span><br><span class="line">    <span class="keyword">const</span> PI = <span class="number">3.14</span></span><br></pre></td></tr></table></figure></li>
<li>常量赋值后，不能修改值,但要看情况，基本数据类型不能修改，数组对象等修改其内容是允许的<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//第一种情况：基本数据类型</span></span><br><span class="line">    <span class="keyword">const</span> MI = <span class="number">3.14</span></span><br><span class="line">    <span class="comment">//报错，常量不能被修改</span></span><br><span class="line">    <span class="comment">//MI = 10</span></span><br><span class="line"></span><br><span class="line">    <span class="comment">//第二种情况:复杂数据类型，可以修改数据结构内部的值，不能修改复杂数据本身</span></span><br><span class="line">    <span class="keyword">const</span> ary = [<span class="number">100</span>,<span class="number">200</span>]</span><br><span class="line">    <span class="comment">//可以</span></span><br><span class="line">    ary[<span class="number">0</span>] = <span class="number">300</span></span><br><span class="line">    <span class="comment">//不行</span></span><br><span class="line">    <span class="comment">//ary = [500,600]</span></span><br><span class="line">    <span class="comment">//总结：常量值对应的内存地址不可修改</span></span><br></pre></td></tr></table></figure>
<h1 id="解构赋值"><a href="#解构赋值" class="headerlink" title="解构赋值"></a>解构赋值</h1></li>
<li>内容：数组解构允许我们按照一一对应的关系从数组中提取值然后将值赋值给变量</li>
<li>数组解构<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//数组解构</span></span><br><span class="line">   <span class="keyword">let</span> aaa = [<span class="number">1</span>,<span class="number">2</span>,<span class="number">3</span>]</span><br><span class="line">   <span class="keyword">let</span> [a,b,c] = aaa</span><br><span class="line">   <span class="built_in">console</span>.log(a)</span><br><span class="line">   <span class="built_in">console</span>.log(b)</span><br><span class="line">   <span class="built_in">console</span>.log(c)</span><br><span class="line">   <span class="comment">//不一一对应的时候</span></span><br><span class="line">   <span class="comment">//let [food] = []</span></span><br><span class="line">   <span class="comment">//food is undefined</span></span><br><span class="line">   <span class="comment">//let [a,b] = [1]</span></span><br><span class="line">   <span class="comment">//b is undefined</span></span><br></pre></td></tr></table></figure></li>
<li>对象结构(允许我们使用变量的名字匹配对象的属性，匹配成功将对象属性的值赋值给变量)<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">let</span> person = &#123;<span class="attr">name</span>:<span class="string">'lisi'</span>,<span class="attr">age</span>:<span class="number">12</span>&#125;</span><br><span class="line">  <span class="keyword">let</span> &#123;name,age&#125; = person</span><br><span class="line">  <span class="built_in">console</span>.log(name)</span><br><span class="line">  <span class="built_in">console</span>.log(age)</span><br><span class="line">  <span class="comment">//第二种方式 myName myAge属于别名</span></span><br><span class="line">  <span class="keyword">let</span>&#123;<span class="attr">name</span>:myName,<span class="attr">age</span>:myAge&#125; = person</span><br><span class="line">  <span class="built_in">console</span>.log(myName)</span><br><span class="line">  <span class="built_in">console</span>.log(myAge)</span><br></pre></td></tr></table></figure>
<h1 id="箭头函数"><a href="#箭头函数" class="headerlink" title="箭头函数"></a>箭头函数</h1></li>
<li>内容：ES6中新增的定义函数的方式,用来简化函数定义语法<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> fn = <span class="function"><span class="params">()</span> =&gt;</span> &#123;</span><br><span class="line">	<span class="built_in">console</span>.log(<span class="string">'111'</span>)</span><br><span class="line">&#125;</span><br><span class="line">fn();</span><br></pre></td></tr></table></figure></li>
<li>函数中只有一句代码，且代码的执行结果就是返回值，可以省略大括号<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> fn1 = <span class="function">(<span class="params">num1,num2</span>) =&gt;</span> num1 + num2</span><br><span class="line">   <span class="keyword">const</span> r = fn1(<span class="number">1</span>,<span class="number">2</span>)</span><br><span class="line">   <span class="built_in">console</span>.log(r)</span><br></pre></td></tr></table></figure></li>
<li>如果形参只有一个，那么形参外面的小括号可以省略<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> fn2 = <span class="function"><span class="params">v</span> =&gt;</span>&#123;</span><br><span class="line">       <span class="built_in">console</span>.log(v)</span><br><span class="line">   &#125;</span><br><span class="line">   fn2(<span class="number">2</span>)</span><br></pre></td></tr></table></figure></li>
<li>箭头函数不绑定this，箭头函数没有自己的this关键字，如果在箭头函数中使用this，this关键字将指向箭头函数定义位置中的this <figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">ffn</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">    <span class="built_in">console</span>.log(<span class="keyword">this</span>)</span><br><span class="line">    <span class="keyword">return</span> <span class="function"><span class="params">()</span> =&gt;</span> &#123;</span><br><span class="line">        <span class="built_in">console</span>.log(<span class="keyword">this</span>)</span><br><span class="line">    &#125;</span><br><span class="line">   &#125;</span><br><span class="line">   </span><br><span class="line">   <span class="comment">//两个this都是obj对象</span></span><br><span class="line">   <span class="keyword">const</span> obj = &#123;<span class="attr">name</span>:<span class="string">'zhangsan'</span>&#125;</span><br><span class="line">   <span class="keyword">const</span> resFn = ffn.call(obj)</span><br><span class="line">   resFn()</span><br></pre></td></tr></table></figure></li>
<li>看一个面试题，理解一下这个this<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//分析：箭头函数没有this关键字，且oobj对象不能产生作用域，所以say方法实际上是被定义在了全局作用域下，</span></span><br><span class="line">   <span class="comment">//所以this指代的是window对象，没有aage值，所以为undefined，</span></span><br><span class="line">   <span class="comment">//var aage = 100    若添加全局的变量aage值，则可以读取值为100</span></span><br><span class="line">   <span class="keyword">var</span> oobj = &#123;</span><br><span class="line">       aage:<span class="number">20</span>,</span><br><span class="line">       say: <span class="function"><span class="params">()</span> =&gt;</span>&#123;</span><br><span class="line">           <span class="built_in">console</span>.log(<span class="keyword">this</span>.aage)</span><br><span class="line">       &#125;</span><br><span class="line">   &#125;</span><br><span class="line">   oobj.say()</span><br></pre></td></tr></table></figure>
<h1 id="剩余参数"><a href="#剩余参数" class="headerlink" title="剩余参数"></a>剩余参数</h1></li>
<li>ES6中允许我们将一个不定数量的参数表示为一个数组,称之为剩余参数<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> sum = <span class="function">(<span class="params">...args</span>) =&gt;</span>&#123;</span><br><span class="line">      <span class="keyword">let</span> total = <span class="number">0</span></span><br><span class="line">      args.forEach(<span class="function"><span class="params">item</span> =&gt;</span> total += item)</span><br><span class="line">      <span class="keyword">return</span> total</span><br><span class="line">   &#125;</span><br><span class="line">   <span class="built_in">console</span>.log(sum(<span class="number">10</span>,<span class="number">20</span>))</span><br><span class="line">   <span class="built_in">console</span>.log(sum(<span class="number">10</span>,<span class="number">20</span>,<span class="number">30</span>))</span><br></pre></td></tr></table></figure></li>
<li>剩余参数和数组解构配合使用<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">let</span> ary1 = [<span class="string">'张三'</span>,<span class="string">'李四'</span>,<span class="string">'王五'</span>];</span><br><span class="line"><span class="keyword">let</span> [s1,...s2] = ary1;</span><br><span class="line"><span class="built_in">console</span>.log(s1);    <span class="comment">//'张三'   </span></span><br><span class="line"><span class="built_in">console</span>.log(s2);   <span class="comment">//'李四' '王五'</span></span><br></pre></td></tr></table></figure>
<h1 id="扩展运算符"><a href="#扩展运算符" class="headerlink" title="扩展运算符"></a>扩展运算符</h1></li>
<li>内容：扩展运算符，又叫展开语法，可以将数组或者对象转为逗号分隔的参数序列<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">let</span> ary2 = [<span class="string">'a'</span>,<span class="string">'b'</span>,<span class="string">'c'</span>]</span><br><span class="line"><span class="comment">//...ary2 相当于 'a','b','c'</span></span><br><span class="line"><span class="built_in">console</span>.log(...ary2)  <span class="comment">//相当于console.log('a','b','c')，但是打印出来是1 2 3 没有逗号</span></span><br></pre></td></tr></table></figure></li>
<li>扩展运算符可以用于合并数组<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">let</span> ary3 = [<span class="number">1</span>,<span class="number">2</span>,<span class="number">3</span>]</span><br><span class="line"><span class="keyword">let</span> ary4 = [<span class="number">4</span>,<span class="number">5</span>,<span class="number">6</span>]</span><br><span class="line"><span class="comment">//方法1</span></span><br><span class="line">ary3.push(...ary4)</span><br><span class="line"><span class="comment">//方法2</span></span><br><span class="line"><span class="keyword">let</span> ary5 = [...ary3,...ary4]</span><br><span class="line"><span class="built_in">console</span>.log(ary3)</span><br><span class="line"><span class="built_in">console</span>.log(ary5)</span><br></pre></td></tr></table></figure></li>
<li>利用扩展运算符将伪数组转换为真正的数组<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> oDivs = <span class="built_in">document</span>.getElementsByTagName(<span class="string">'div'</span>)</span><br><span class="line"><span class="built_in">console</span>.log(oDivs)</span><br><span class="line">oDivs = [...oDivs]  <span class="comment">//将伪数组转换为真数组</span></span><br><span class="line"><span class="built_in">console</span>.log(oDivs)</span><br></pre></td></tr></table></figure>
<h1 id="构造函数方法"><a href="#构造函数方法" class="headerlink" title="构造函数方法"></a>构造函数方法</h1></li>
<li>Array.from()可以将伪数组转换为真数组,方法还可以接受第二个参数，作用类似于数组的map方法，用来对每个元素进行处理，将处理后的值放入返回的数组<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">let</span> arrayLike = &#123;</span><br><span class="line">        <span class="string">'0'</span>:<span class="number">1</span>,</span><br><span class="line">        <span class="string">'1'</span>:<span class="number">2</span>,</span><br><span class="line">        <span class="string">'length'</span>:<span class="number">2</span></span><br><span class="line">    &#125;</span><br><span class="line"><span class="keyword">let</span> ary6 = <span class="built_in">Array</span>.from(arrayLike,item=&gt;item*<span class="number">2</span>)</span><br><span class="line"><span class="built_in">console</span>.log(ary6)</span><br></pre></td></tr></table></figure>
<h1 id="新增实例方法"><a href="#新增实例方法" class="headerlink" title="新增实例方法"></a>新增实例方法</h1></li>
<li>实例方法find(),接收一个函数作为参数，用于找出第一个符合条件的数组成员，如果没有找到返回undefined<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> ary7 = [</span><br><span class="line">        &#123;</span><br><span class="line">            id:<span class="number">1</span>,</span><br><span class="line">            name:<span class="string">'张三'</span></span><br><span class="line">        &#125;,</span><br><span class="line">        &#123;</span><br><span class="line">            id:<span class="number">2</span>,</span><br><span class="line">            name:<span class="string">'李四'</span></span><br><span class="line">        &#125;,</span><br><span class="line">    ]</span><br><span class="line">    <span class="keyword">let</span> target = ary7.find(<span class="function"><span class="params">item</span> =&gt;</span> item.id == <span class="number">2</span>)</span><br><span class="line">    <span class="built_in">console</span>.log(target)</span><br></pre></td></tr></table></figure></li>
<li>实例方法findIndex(),接收一个函数作为参数，用于找出第一个符合条件的数组成员的位置，如果没有找到返回-1<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> ary8 = [<span class="number">10</span>,<span class="number">20</span>,<span class="number">30</span>]</span><br><span class="line"><span class="keyword">let</span> index = ary8.findIndex(<span class="function"><span class="params">item</span> =&gt;</span> item &gt; <span class="number">20</span>)</span><br><span class="line"><span class="built_in">console</span>.log(index)</span><br></pre></td></tr></table></figure></li>
<li>实例方法includes()，表示某个数组是否包含给定的值，返回布尔值<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> ary9 = [<span class="string">'a'</span>,<span class="string">'b'</span>,<span class="string">'c'</span>]</span><br><span class="line"><span class="keyword">let</span> re = ary9.includes(<span class="string">'a'</span>)</span><br><span class="line"><span class="built_in">console</span>.log(re)</span><br></pre></td></tr></table></figure>
<h1 id="模板字符串"><a href="#模板字符串" class="headerlink" title="模板字符串"></a>模板字符串</h1>反引号定义(esc下面那个键)</li>
<li>可以解析变量(使用${})<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> nameyou = <span class="string">'lockme'</span></span><br><span class="line"><span class="keyword">let</span> you = <span class="string">`这是一个模板字符串<span class="subst">$&#123;nameyou&#125;</span>`</span></span><br><span class="line"><span class="built_in">console</span>.log(you)</span><br></pre></td></tr></table></figure></li>
<li>可以换行<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> res = &#123;</span><br><span class="line">    name:<span class="string">'张三'</span>,</span><br><span class="line">    age:<span class="number">18</span>,</span><br><span class="line">&#125;</span><br><span class="line"><span class="keyword">var</span> html = <span class="string">`</span></span><br><span class="line"><span class="string">&lt;div&gt;</span></span><br><span class="line"><span class="string">    &lt;span&gt;<span class="subst">$&#123;res.name&#125;</span>&lt;/span&gt;</span></span><br><span class="line"><span class="string">    &lt;span&gt;<span class="subst">$&#123;res.age&#125;</span>&lt;/span&gt;</span></span><br><span class="line"><span class="string">&lt;/div&gt;</span></span><br><span class="line"><span class="string">`</span> </span><br><span class="line"><span class="built_in">console</span>.log(html)</span><br></pre></td></tr></table></figure></li>
<li>可以调用函数，显示的是函数的返回结果(真的贼方便)<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">fnn</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">     <span class="keyword">return</span> <span class="string">'这是一个函数的返回结果'</span></span><br><span class="line"> &#125;</span><br><span class="line"> <span class="keyword">var</span> hh = <span class="string">`这是一个模板字符串<span class="subst">$&#123;fnn()&#125;</span>`</span></span><br><span class="line"> <span class="built_in">console</span>.log(hh)</span><br></pre></td></tr></table></figure>
<h1 id="数据结构Set"><a href="#数据结构Set" class="headerlink" title="数据结构Set"></a>数据结构Set</h1>数据结构Set类似于数组，但不会记录重复的值</li>
<li>Set本身是一个构造函数，用来创建Set数据结构<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> s = <span class="keyword">new</span> <span class="built_in">Set</span>()</span><br><span class="line"><span class="built_in">console</span>.log(s.size)</span><br></pre></td></tr></table></figure></li>
<li>Set函数可以接收一个数组作为参数，用来初始化Set的值<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> ss = <span class="keyword">new</span> <span class="built_in">Set</span>([<span class="number">1</span>,<span class="number">2</span>,<span class="number">3</span>])</span><br><span class="line"><span class="built_in">console</span>.log(ss.size)</span><br></pre></td></tr></table></figure></li>
<li>利用Set可以实现数组去重<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> <span class="keyword">set</span> = new Set(['a','a','b','b']) //将数组变成<span class="keyword">set</span>结构</span><br><span class="line">const sss = [...<span class="keyword">set</span>]     //展开并转成数组</span><br><span class="line">console.log(sss)         // 'a'  'b'</span><br></pre></td></tr></table></figure></li>
<li>Set数据结构的实例方法<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//Set数据结构的实例方法</span></span><br><span class="line"><span class="comment">//add(value)添加某个值，返回Set结构本身,此方法可以链式调用</span></span><br><span class="line"><span class="comment">//delete(value)删除某个值，返回布尔值</span></span><br><span class="line"><span class="comment">//has(value)返回一个布尔值，表示该值是否为Set的成员</span></span><br><span class="line"><span class="comment">//clear()清除所有成员，没有返回值</span></span><br><span class="line"><span class="comment">//forEach(),遍历Set</span></span><br><span class="line"><span class="keyword">const</span> s4 = <span class="keyword">new</span> <span class="built_in">Set</span>()</span><br><span class="line">s4.add(<span class="string">'a'</span>).add(<span class="string">'b'</span>)</span><br><span class="line"><span class="built_in">console</span>.log(s4.size)</span><br><span class="line">s4.forEach(<span class="function"><span class="params">value</span> =&gt;</span>&#123;</span><br><span class="line">    <span class="built_in">console</span>.log(value)</span><br><span class="line">&#125;)</span><br><span class="line"><span class="keyword">let</span> resu = s4.delete(<span class="string">'a'</span>)</span><br><span class="line"><span class="built_in">console</span>.log(resu)</span><br><span class="line"><span class="built_in">console</span>.log(s4.has(<span class="string">'c'</span>))</span><br><span class="line">s4.clear()</span><br><span class="line"><span class="built_in">console</span>.log(s4.size)</span><br></pre></td></tr></table></figure>
好了，我的分享就到这里了，谢谢大家</li>
</ol>

      
      <!-- reward -->
      
      <div id="reward-btn">
        打赏
      </div>
      
    </div>
    
    
      <!-- copyright -->
      
        <div class="declare">
          <ul class="post-copyright">
            <li>
              <i class="ri-copyright-line"></i>
              <strong>版权声明： </strong s>
              本博客所有文章除特别声明外，均采用 <a href="https://www.apache.org/licenses/LICENSE-2.0.html" rel="external nofollow"
                target="_blank">Apache License 2.0</a> 许可协议。转载请注明出处！
            </li>
          </ul>
        </div>
        
    <footer class="article-footer">
      
          
<div class="share-btn">
      <span class="share-sns share-outer">
        <i class="ri-share-forward-line"></i>
        分享
      </span>
      <div class="share-wrap">
        <i class="arrow"></i>
        <div class="share-icons">
          
          <a class="weibo share-sns" href="javascript:;" data-type="weibo">
            <i class="ri-weibo-fill"></i>
          </a>
          <a class="weixin share-sns wxFab" href="javascript:;" data-type="weixin">
            <i class="ri-wechat-fill"></i>
          </a>
          <a class="qq share-sns" href="javascript:;" data-type="qq">
            <i class="ri-qq-fill"></i>
          </a>
          <a class="douban share-sns" href="javascript:;" data-type="douban">
            <i class="ri-douban-line"></i>
          </a>
          <!-- <a class="qzone share-sns" href="javascript:;" data-type="qzone">
            <i class="icon icon-qzone"></i>
          </a> -->
          
          <a class="facebook share-sns" href="javascript:;" data-type="facebook">
            <i class="ri-facebook-circle-fill"></i>
          </a>
          <a class="twitter share-sns" href="javascript:;" data-type="twitter">
            <i class="ri-twitter-fill"></i>
          </a>
          <a class="google share-sns" href="javascript:;" data-type="google">
            <i class="ri-google-fill"></i>
          </a>
        </div>
      </div>
</div>

<div class="wx-share-modal">
    <a class="modal-close" href="javascript:;"><i class="ri-close-circle-line"></i></a>
    <p>扫一扫，分享到微信</p>
    <div class="wx-qrcode">
      <img src="//api.qrserver.com/v1/create-qr-code/?size=150x150&data=https://lockme.gitee.io/2020/04/12/ES6-md/" alt="微信分享二维码">
    </div>
</div>

<div id="share-mask"></div>
      
      
  <ul class="article-tag-list" itemprop="keywords"><li class="article-tag-list-item"><a class="article-tag-list-link" href="/tags/ES6/" rel="tag">ES6</a></li><li class="article-tag-list-item"><a class="article-tag-list-link" href="/tags/%E5%A4%8D%E4%B9%A0/" rel="tag">复习</a></li><li class="article-tag-list-item"><a class="article-tag-list-link" href="/tags/%E6%8A%80%E6%9C%AF/" rel="tag">技术</a></li></ul>


    </footer>

  </div>

  
  
  <nav class="article-nav">
    
      <a href="/2020/04/12/JS-HTML-DOM-md/" class="article-nav-link">
        <strong class="article-nav-caption">上一篇</strong>
        <div class="article-nav-title">
          
            JS HTML DOM.md
          
        </div>
      </a>
    
    
      <a href="/2020/04/12/JS-Function-md/" class="article-nav-link">
        <strong class="article-nav-caption">下一篇</strong>
        <div class="article-nav-title">JS Function.md</div>
      </a>
    
  </nav>


  

  
  
<!-- valine评论 -->
<div id="vcomments-box">
    <div id="vcomments">
    </div>
</div>
<script src="//cdn1.lncld.net/static/js/3.0.4/av-min.js"></script>
<script src='https://cdn.jsdelivr.net/npm/valine@1.3.10/dist/Valine.min.js'></script>
<script>
    new Valine({
        el: '#vcomments',
        app_id: 'pmaQYnbUE9O0H1hiYKLiec4R-gzGzoHsz',
        app_key: 'LMujtI79W4vgEQ3Az5IMo50y',
        path: window.location.pathname,
        notify: 'false',
        verify: 'false',
        avatar: 'mp',
        placeholder: '给我的文章加点评论吧~',
        recordIP: true
    });
    const infoEle = document.querySelector('#vcomments .info');
    if (infoEle && infoEle.childNodes && infoEle.childNodes.length > 0) {
        infoEle.childNodes.forEach(function (item) {
            item.parentNode.removeChild(item);
        });
    }
</script>
<style>
    #vcomments-box {
        padding: 5px 30px;
    }

    @media screen and (max-width: 800px) {
        #vcomments-box {
            padding: 5px 0px;
        }
    }

    #vcomments-box #vcomments {
        background-color: #fff;
    }

    .v .vlist .vcard .vh {
        padding-right: 20px;
    }

    .v .vlist .vcard {
        padding-left: 10px;
    }
</style>

  

  
  
  

</article>
</section>
      <footer class="footer">
  <div class="outer">
    <ul class="list-inline">
      <li>
        &copy;
        2020
        月下ctrlc
      </li>
      <li>
        
        Powered by
        
        
        <a href="https://hexo.io" target="_blank">Hexo</a> Theme <a href="https://github.com/Shen-Yu/hexo-theme-ayer" target="_blank">Ayer</a>
        
      </li>
    </ul>
    <ul class="list-inline">
      <li>
        
        
        <span>
  <i>PV:<span id="busuanzi_value_page_pv"></span></i>
  <i>UV:<span id="busuanzi_value_site_uv"></span></i>
</span>
        
      </li>
      
      <li>
        <!-- cnzz统计 -->
        
        <script type="text/javascript" src='https://s9.cnzz.com/z_stat.php?id=1278069914&amp;web_id=1278069914'></script>
        
      </li>
    </ul>
  </div>
</footer>
      <div class="to_top">
        <div class="totop" id="totop">
  <i class="ri-arrow-up-line"></i>
</div>
      </div>
    </main>
    <aside class="sidebar">
      <button class="navbar-toggle"></button>
<nav class="navbar">
  
  <div class="logo">
    <a href="/"><img src="/images/avatar.jpg" alt="Small Bridge"></a>
  </div>
  
  <ul class="nav nav-main">
    
    <li class="nav-item">
      <a class="nav-item-link" href="/">主页</a>
    </li>
    
    <li class="nav-item">
      <a class="nav-item-link" href="/archives">归档</a>
    </li>
    
    <li class="nav-item">
      <a class="nav-item-link" href="/categories">分类</a>
    </li>
    
    <li class="nav-item">
      <a class="nav-item-link" href="/tags">标签</a>
    </li>
    
    <li class="nav-item">
      <a class="nav-item-link" href="/tags/Vue/">Vue</a>
    </li>
    
    <li class="nav-item">
      <a class="nav-item-link" href="http://shenyu-vip.lofter.com" target="_blank" rel="noopener">主题作者相册</a>
    </li>
    
    <li class="nav-item">
      <a class="nav-item-link" href="/2020/03/18/about">关于我</a>
    </li>
    
  </ul>
</nav>
<nav class="navbar navbar-bottom">
  <ul class="nav">
    <li class="nav-item">
      
      <a class="nav-item-link nav-item-search"  title="Search">
        <i class="ri-search-line"></i>
      </a>
      
      
      <a class="nav-item-link" target="_blank" href="/atom.xml" title="RSS Feed">
        <i class="ri-rss-line"></i>
      </a>
      
    </li>
  </ul>
</nav>
<div class="search-form-wrap">
  <div class="local-search local-search-plugin">
  <input type="search" id="local-search-input" class="local-search-input" placeholder="Search...">
  <div id="local-search-result" class="local-search-result"></div>
</div>
</div>
    </aside>
    <div id="mask"></div>

<!-- #reward -->
<div id="reward">
  <span class="close"><i class="ri-close-line"></i></span>
  <p class="reward-p"><i class="ri-cup-line"></i>拿钱砸死我吧~</p>
  <div class="reward-box">
    
    <div class="reward-item">
      <img class="reward-img" src="/images/alipay.png">
      <span class="reward-type">支付宝</span>
    </div>
    
    
    <div class="reward-item">
      <img class="reward-img" src="/images/wechat.png">
      <span class="reward-type">微信</span>
    </div>
    
  </div>
</div>
    
<script src="/js/jquery-2.0.3.min.js"></script>


<script src="/js/share.js"></script>


<script src="/js/lazyload.min.js"></script>


<script>
  try {
    var typed = new Typed("#subtitle", {
      strings: ['面朝大海，春暖花开', '愿你一生努力，一生被爱', '想要的都拥有，得不到的都释怀'],
      startDelay: 0,
      typeSpeed: 200,
      loop: true,
      backSpeed: 100,
      showCursor: true
    });
  } catch (err) {
  }

</script>




<script src="/js/tocbot.min.js"></script>

<script>
  // Tocbot_v4.7.0  http://tscanlin.github.io/tocbot/
  tocbot.init({
    tocSelector: '.tocbot',
    contentSelector: '.article-entry',
    headingSelector: 'h1, h2, h3, h4, h5, h6',
    hasInnerContainers: true,
    scrollSmooth: true,
    scrollContainer: 'main',
    positionFixedSelector: '.tocbot',
    positionFixedClass: 'is-position-fixed',
    fixedSidebarOffset: 'auto',
    onClick: (e) => {
      $('.toc-link').removeClass('is-active-link');
      $(`a[href=${e.target.hash}]`).addClass('is-active-link');
      $(e.target.hash).scrollIntoView();
      return false;
    }
  });
</script>



<script src="https://cdn.jsdelivr.net/npm/jquery-modal@0.9.2/jquery.modal.min.js"></script>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/jquery-modal@0.9.2/jquery.modal.min.css">
<script src="https://cdn.jsdelivr.net/npm/justifiedGallery@3.7.0/dist/js/jquery.justifiedGallery.min.js"></script>

<script src="/js/ayer.js"></script>



<!-- Root element of PhotoSwipe. Must have class pswp. -->
<div class="pswp" tabindex="-1" role="dialog" aria-hidden="true">

    <!-- Background of PhotoSwipe. 
         It's a separate element as animating opacity is faster than rgba(). -->
    <div class="pswp__bg"></div>

    <!-- Slides wrapper with overflow:hidden. -->
    <div class="pswp__scroll-wrap">

        <!-- Container that holds slides. 
            PhotoSwipe keeps only 3 of them in the DOM to save memory.
            Don't modify these 3 pswp__item elements, data is added later on. -->
        <div class="pswp__container">
            <div class="pswp__item"></div>
            <div class="pswp__item"></div>
            <div class="pswp__item"></div>
        </div>

        <!-- Default (PhotoSwipeUI_Default) interface on top of sliding area. Can be changed. -->
        <div class="pswp__ui pswp__ui--hidden">

            <div class="pswp__top-bar">

                <!--  Controls are self-explanatory. Order can be changed. -->

                <div class="pswp__counter"></div>

                <button class="pswp__button pswp__button--close" title="Close (Esc)"></button>

                <button class="pswp__button pswp__button--share" style="display:none" title="Share"></button>

                <button class="pswp__button pswp__button--fs" title="Toggle fullscreen"></button>

                <button class="pswp__button pswp__button--zoom" title="Zoom in/out"></button>

                <!-- Preloader demo http://codepen.io/dimsemenov/pen/yyBWoR -->
                <!-- element will get class pswp__preloader--active when preloader is running -->
                <div class="pswp__preloader">
                    <div class="pswp__preloader__icn">
                        <div class="pswp__preloader__cut">
                            <div class="pswp__preloader__donut"></div>
                        </div>
                    </div>
                </div>
            </div>

            <div class="pswp__share-modal pswp__share-modal--hidden pswp__single-tap">
                <div class="pswp__share-tooltip"></div>
            </div>

            <button class="pswp__button pswp__button--arrow--left" title="Previous (arrow left)">
            </button>

            <button class="pswp__button pswp__button--arrow--right" title="Next (arrow right)">
            </button>

            <div class="pswp__caption">
                <div class="pswp__caption__center"></div>
            </div>

        </div>

    </div>

</div>

<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/photoswipe@4.1.3/dist/photoswipe.min.css">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/photoswipe@4.1.3/dist/default-skin/default-skin.css">
<script src="https://cdn.jsdelivr.net/npm/photoswipe@4.1.3/dist/photoswipe.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/photoswipe@4.1.3/dist/photoswipe-ui-default.min.js"></script>

<script>
    function viewer_init() {
        let pswpElement = document.querySelectorAll('.pswp')[0];
        let $imgArr = document.querySelectorAll(('.article-entry img:not(.reward-img)'))

        $imgArr.forEach(($em, i) => {
            $em.onclick = () => {
                // slider展开状态
                // todo: 这样不好，后面改成状态
                if (document.querySelector('.left-col.show')) return
                let items = []
                $imgArr.forEach(($em2, i2) => {
                    let img = $em2.getAttribute('data-idx', i2)
                    let src = $em2.getAttribute('data-target') || $em2.getAttribute('src')
                    let title = $em2.getAttribute('alt')
                    // 获得原图尺寸
                    const image = new Image()
                    image.src = src
                    items.push({
                        src: src,
                        w: image.width || $em2.width,
                        h: image.height || $em2.height,
                        title: title
                    })
                })
                var gallery = new PhotoSwipe(pswpElement, PhotoSwipeUI_Default, items, {
                    index: parseInt(i)
                });
                gallery.init()
            }
        })
    }
    viewer_init()
</script>


<script type="text/x-mathjax-config">
  MathJax.Hub.Config({
      tex2jax: {
          inlineMath: [ ['$','$'], ["\\(","\\)"]  ],
          processEscapes: true,
          skipTags: ['script', 'noscript', 'style', 'textarea', 'pre', 'code']
      }
  });

  MathJax.Hub.Queue(function() {
      var all = MathJax.Hub.getAllJax(), i;
      for(i=0; i < all.length; i += 1) {
          all[i].SourceElement().parentNode.className += ' has-jax';
      }
  });
</script>

<script src="https://cdn.jsdelivr.net/npm/mathjax@2.7.6/unpacked/MathJax.js?config=TeX-AMS-MML_HTMLorMML"></script>
<script>
  var ayerConfig = {
    mathjax: true
  }
</script>



<script src="/js/busuanzi-2.3.pure.min.js"></script>



<script type="text/javascript" src="https://js.users.51.la/20544303.js"></script>


    
  </div>
</body>

</html>